package com.lagou.demo.service.impl;

import com.lagou.demo.service.IUserService;
import com.lagou.edu.mvcframework.annotations.LagouService;
import com.lagou.edu.mvcframework.pojo.User;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@LagouService("userService")
public class UserServiceImpl implements IUserService {

    private static List<User> userList = new ArrayList<User>();

    static {
        User jack = new User();
        jack.setId(1L);
        jack.setUsername("Tom");
        jack.setAge(18);
        jack.setSex("f");
        String[] roles = {"ROLE_USER"};
        jack.setRoles(roles);

        userList.add(jack);
    }

    @Override
    public User findByUsername(String username) {
        Optional<User> userOptional = userList.stream().filter(u -> u.getUsername().equals(username)).findFirst();
        if (userOptional.isPresent()) {
            return userOptional.get();
        }
        return null;
    }
}
